#!/usr/bin/env python
#coding:utf-8

'''
title：k3按键中断响应恢复静态IPv4.2
author：weiyc
site：http://www.hysdqx.com/
'''

import Jetson.GPIO as GPIO  #引入GPIO模块
import time                 #引入time库
import os
import getpass
import warnings
warnings.filterwarnings("ignore")

touchPin = 18  #SPI2_CS0  Sysfs GPIO:gpio15  BCM:24  BOARD:18  wiringPi:5 
#flag_event_detected = 0
#flag_event_count = 0

GPIO.setmode(GPIO.BOARD)
GPIO.setup(touchPin, GPIO.IN)

def password():
    #password = getpass.getpass("Please input the administrator's password: ")
    return 'nvidia'
        
def commandTouchInterfaces(cmd):
    #text = os.popen( "echo %s | sudo -S %s" % (password(),cmd) ).read()
    #defaultInterfaces = os.system( "echo %s | sudo -S %s" % (password(),cmd) )
    os.system( "echo %s | sudo -S %s" % (password(),cmd) )
    #return defaultInterfaces
    
def commandChmodInterfaces(cmd):
    #text = os.popen( "echo %s | sudo -S %s" % (password(),cmd) ).read()
    #defaultInterfaces = os.system( "echo %s | sudo -S %s" % (password(),cmd) )
    os.system( "echo %s | sudo -S %s" % (password(),cmd) )
    #return defaultInterfaces
  
def commandCpInterfaces(cmd):
    #text = os.popen( "echo %s | sudo -S %s" % (password(),cmd) ).read()
    #defaultInterfaces = os.system( "echo %s | sudo -S %s" % (password(),cmd) )
    os.system( "echo %s | sudo -S %s" % (password(),cmd) )
    #return defaultInterfaces  

def getRespose(self):
    #os.system('touch /etc/network/interfaces')
    #commandTouchInterfaces('touch /etc/network/interfaces')
    #os.system('chmod 777 /etc/network/interfaces')
    #commandChmodInterfaces('chmod 777 /etc/network/interfaces')
    #commandCpInterfaces('cp -rf /home/nvidia/HYSD_shell/interfaces /etc/network/')
    #global flag_event_count 
    #flag_event_count = flag_event_count + 1
    time.sleep( 5 )
    #if flag_event_count % 2 != 0:
    GPIO_inputNum = GPIO.input(touchPin)
    #print (GPIO_inputNum)
    if GPIO_inputNum == 0:
        commandCpInterfaces('cp -rf /home/nvidia/HYSD_shell/interfaces /etc/network/')  #检测到事件触发回调函数
        time.sleep( 1 )
        os.system( "echo %s | sudo -S %s" % (password(),'reboot') )
    #flag_event_count = 0
    #if flag_event_detected == 0:
        #time.sleep( 5 )
        #flag_event_detected = 1
    #if ( flag_event_count % 2 == 0 ) and ( flag_event_detected == 1 ): 
        #commandCpInterfaces('cp -rf /home/nvidia/HYSD_shell/interfaces /etc/network/'))  #检测到事件触发回调函数
    #global flag_event_detected
    #flag_event_detected = 1
    #print (flag_event_detected)

#GPIO.add_event_detect(DeskCycle.PIN, GPIO.FALLING, callback=self.pin_event,bouncetime=100) 
GPIO.add_event_detect(touchPin, GPIO.FALLING,callback=getRespose,bouncetime=1000)  #注册一个检测电压由高变低事件 BOTH FALLING RISING
#GPIO.add_event_callback(touchPin, getRespose)  #检测到事件触发回调getRespose 函数
#GPIO.event_detected(touchPin)
#print ("开始检测")

while True:
    #if GPIO.event_detected(touchPin):
        #flag_event_count = flag_event_count + 1
        #print ("检测到事件")
    #if flag_event_detected == 1:
        #print ("检测到事件")
    time.sleep(4)  #进行挂起4秒
    #print ("接下来的动作")

